/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.odbs.controller;

import com.etop.odbs.beans.Document;
import com.etop.odbs.beans.OdbsException;
import com.etop.odbs.beans.User;
import com.etop.odbs.service.UserService;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author cjh
 */
@Controller
@SessionAttributes("loginUser")
public class UserController {

    @Resource(name = "userService")
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = {"/load"}, method = RequestMethod.GET)
    public String load() {
        return "user/load";
    }

    @RequestMapping(value = {"/load"}, method = RequestMethod.POST)
    public String load(String key, String password, Model model) {
        User u = null;
        try {
            u = userService.load(key, password);
        } catch (OdbsException oe) {
            model.addAttribute("errorMsg", oe.getMessage());
            return "user/load";
        }
        model.addAttribute("loginUser", u);
        return "redirect:/index";
    }

    @RequestMapping(value = {"/index"}, method = RequestMethod.GET)
    public String index() {
        return "user/index";
    }

    @RequestMapping(value = {"/upload"}, method = RequestMethod.GET)
    public String upload(Model model) {
        model.addAttribute("document", new Document());
        return "user/upload";
    }

    @RequestMapping(value = {"/upload"}, method = RequestMethod.POST)
    public String upload(@RequestParam(value = "uploadFile", required = true) MultipartFile[] uploadFile,
            HttpServletRequest request, String[] filename, String[] description, int[] isPublic, 
            String[] classification, int[] point) {

        Document doc;
        User user = (User) request.getSession().getAttribute("loginUser");
        String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");

        for (int i = 0; i < uploadFile.length - 1; i++) {
            System.out.println(uploadFile[i].getOriginalFilename());
            user.setPoint(user.getPoint() + 2);
            //userService.updataUser(user);
            doc = new Document();
            doc.setUser(user);
            doc.setClassification(classification[i]);
            doc.setIsPublic(isPublic[i]);
            doc.setDescription(description[i]);
            doc.setFilename(filename[i]);
            doc.setPoint(point[i]);
            doc.setUploadTime(new Date());
            doc.setPath(realPath + "/" + uploadFile[i].getOriginalFilename());

            userService.addDocument(doc, uploadFile[i]);
        }
        return "redirect:/uploadSuccess";
    }

    @RequestMapping(value = {"/uploadSuccess"}, method = RequestMethod.GET)
    public String uploadSuccess() {
        return "user/uploadSuccess";
    }
}
